package com.bruce.learnrecylerview.app.action;

import com.bruce.commonutils.RxUtils;
import com.bruce.learnrecycler.kotlin.ui.listitem.Item;
import com.bruce.learnrecylerview.app.R;
import com.bruce.learnrecylerview.app.data.WebApi;
import com.bruce.learnrecylerview.app.model.shjt.carmonitor.Car;
import com.bruce.learnrecylerview.app.model.shjt.carmonitor.Result;
import com.bruce.learnrecylerview.app.model.shjt.getline.LineInfoDetail;
import com.bruce.learnrecylerview.app.model.shjt.getline.Stop;
import com.github.bluzwang.fluxsupport.Dispatcher;
import rx.Observable;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangzhijie@wind-mobi.com on 2015/9/1.
 */
@Deprecated
public class ActionDelegate {
    final Dispatcher dispatcher;
    final WebApi api;

    @Inject
    public ActionDelegate(Dispatcher dispatcher, WebApi api) {
        this.dispatcher = dispatcher;
        this.api = api;
    }

    public void register(Object object) {
        dispatcher.register(object);
    }

    public void unregister(Object object) {
        dispatcher.unregister(object);
    }

    /**
     * 根据线路名称找线路具体信息
     *
     * @param lineName
     * @param isPx
     * @return
     */
    public Observable<LineInfoDetail> requestLineInfoDetailByName(String lineName, boolean isPx) {
        return api.requestLineInfo(lineName, isPx)
                .flatMap(lineInfo -> api.requestLineInfoDetail(lineInfo.getLine_id(), isPx));
    }

    /**
     * 根据站名 在detail中找出相应的准确站名 没有则返回空数组
     *
     * @param detail
     * @param stationName
     * @return
     */
    public List<RxUtils.ABC<String, String, Boolean>> findOutStationInDetail(LineInfoDetail detail, String stationName) {
        List<RxUtils.ABC<String, String, Boolean>> stops = new ArrayList<>();
        for (Stop stop : detail.getStops0()) {
            if (stop.getZdmc().equals(stationName)) {
                stops.add(RxUtils.createABC(stop.getId(), detail.lineId, true));
                break;
            }
        }
        for (Stop stop : detail.getStops1()) {
            if (stop.getZdmc().equals(stationName)) {
                stops.add(RxUtils.createABC(stop.getId(), detail.lineId, false));
                break;
            }
        }
        return stops;
    }

    /**
     * 根据线路名 站名找出该站点的准确站名 没有则返回空数组
     *
     * @param lineName
     * @param stationName
     * @param isPx
     * @return
     */
    public Observable<List<RxUtils.ABC<String, String, Boolean>>> requestStationByLineNameAndStationName(String lineName, String stationName, boolean isPx) {
        return requestLineInfoDetailByName(lineName, isPx)
                .map(detail -> findOutStationInDetail(detail, stationName));
    }

    /**
     * 根据线路名 站名 找出该站点的公交实时情况
     *
     * @param lineName
     * @param stationName
     * @param isPx
     * @return
     */
    public Observable<Result> requestResultByLineNameAndStationName(String lineName, String stationName, boolean isPx) {
        return requestStationByLineNameAndStationName(lineName, stationName, isPx)
                .flatMap(Observable::from)
                .flatMap(abc -> api.requestResult(abc.b/*lineid*/, abc.c/*direction*/, abc.a/*stopid*/, isPx));
    }

    public void requestByLineNameStationName(String lineName, String stationName, boolean isPx, int index) {
        requestResultByLineNameAndStationName(lineName, stationName, isPx)
                .map(result -> {
                    if (result.getCars().size() > 0) {
                        Car car = result.getCars().get(0);
                        return new Item(car.getTerminal(), R.layout.base_item, "time", R.mipmap.ic_launcher);
                    }
                    return null;
                })
                .compose(RxUtils.applySchedulers())
                .subscribe(item -> {
                    if (item != null) {
                        dispatcher.dispatchRequestToStoreWithType(MainActions.SHOW_PD_BUS)
                                .and(MainActions.KEY_RESULT_ITEM, item)
                                .and(MainActions.KEY_ITEM_INDEX, index)
                                .post();
                    }
                });
    }
}
